package com.ahammertest.ycgk.jpush;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.shuoyue.ycgk.entity.CourseInfo;
import com.shuoyue.ycgk.ui.course.info.CourseInfoActivity;
import com.shuoyue.ycgk.ui.estimate.EstimateScoreActivity;
import com.shuoyue.ycgk.ui.mok.MokListActivity;
import com.shuoyue.ycgk.ui.news.NewsInfoActivity;
import com.shuoyue.ycgk.ui.papergroups.running.RunningInfoActivity;
import com.shuoyue.ycgk.ui.recruitment.newsinfo.RecurimentInfoActivity;
import com.shuoyue.ycgk.ui.shop.ShopInfoActivity;

import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

public class JPushReceiver extends BroadcastReceiver {
    private NotificationManager nm;
    private static final String TAG = "JPushReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (null == nm) {
            nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        Bundle bundle = intent.getExtras();
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            Log.d(TAG, "JPush 用户注册成功");

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "接受到推送下来的自定义消息");

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "接受到推送下来的通知");
            receivingNotification(context, bundle);

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.d(TAG, "用户点击打开了通知");
            openNotification(context, bundle);

        } else {
            Log.d(TAG, "Unhandled intent - " + intent.getAction());
        }
    }

    private void receivingNotification(Context context, Bundle bundle) {
        String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
        Log.d(TAG, " title : " + title);
        String message = bundle.getString(JPushInterface.EXTRA_ALERT);
        Log.d(TAG, "message : " + message);
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        Log.d(TAG, "extras : " + extras);
//        EventBus.getDefault().post(new TransMessage(REFRESH_MSG_COUNT, bundle));
    }

    private void openNotification(Context context, Bundle bundle) {
//        Intent intent = new Intent(context, MainActivity.class);
//        context.startActivity(intent);
        //
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        try {
            JSONObject extrasJson = new JSONObject(extras);
            String type = extrasJson.optString("type");
            int id = extrasJson.optInt("id");

            switch (type) {
                case "1":
                    //1，课程
                    context.startActivity(new Intent(context, CourseInfoActivity.class)
                            .putExtra("id", id)
                            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                    break;
                case "2":
                    context.startActivity(new Intent(context, ShopInfoActivity.class)
                            .putExtra("id", id).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                    //2图书
                    break;
                case "3":
                    context.startActivity(new Intent(context, RecurimentInfoActivity.class)
                            .putExtra("id", id).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                    //4公告
                    break;
                case "4":
                    context.startActivity(new Intent(context, NewsInfoActivity.class)
                            .putExtra("id", id)
                            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                    //5资讯
                    break;
                case "5":
//                    MokListActivity.start(context, id);
                    context.startActivity(new Intent(context, MokListActivity.class)
                            .putExtra("subjectId", id)
                            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                    //6模考
                    break;
                case "6":
//                    EstimateScoreActivity.start(context, id);
                    context.startActivity(new Intent(context, EstimateScoreActivity.class)
                            .putExtra("id", id)
                            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                    //7估分
                    break;
                case "7":
//                    RunningInfoActivity.start(context, id);
                    context.startActivity(new Intent(context, RunningInfoActivity.class)
                            .putExtra("moduleId", id)
                            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                    //8考前冲刺
                    break;
            }
        } catch (Exception e) {
            Log.w(TAG, "Unexpected: extras is not a valid json", e);
            return;
        }
    }


}
